# config.py - 扩展配置管理
import os
import json
from pathlib import Path

class Config:
    def __init__(self):
        self.app_name = "WebDAV Notes"
        self.version = "3.0.0-alpha"
        
        # 路径配置
        current_dir = Path.cwd()
        self.base_dir = current_dir / ".webdav_notes"
        self.cache_dir = self.base_dir / "cache"
        self.download_cache_dir = self.cache_dir / "download_cache"  # 下载缓存目录
        self.config_file = self.base_dir / "config.json"
        
        # WebDAV 配置
        self.default_webdav_url = "https://webdav.ctfile.com/ws1164253"
        self.data_dir = "data"
        self.notes_dir = "notes"  # 笔记目录
        self.downloadfiles_dir = "downloadfiles"  # 下载文件目录
        
        # 下载管理器配置
        self.admin_password = "dx3906"  # 管理员密码
        self.max_cache_items = 50  # 最大缓存项目数
        self.preload_count = 10    # 预加载数量
        
        # 创建目录
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        self.download_cache_dir.mkdir(parents=True, exist_ok=True)
        
        # 加载用户配置
        self.load_config()
    
    def load_config(self):
        """加载用户配置"""
        if self.config_file.exists():
            try:
                with open(self.config_file, 'r', encoding='utf-8') as f:
                    user_config = json.load(f)
                    self.email = user_config.get('email', '')
                    self.password = user_config.get('password', '')
                    self.webdav_url = user_config.get('webdav_url', self.default_webdav_url)
            except:
                self.email = ''
                self.password = ''
                self.webdav_url = self.default_webdav_url
        else:
            self.email = ''
            self.password = ''
            self.webdav_url = self.default_webdav_url
    
    def save_config(self, email, password, webdav_url=None):
        """保存用户配置"""
        self.email = email
        self.password = password
        if webdav_url:
            self.webdav_url = webdav_url
        
        config_data = {
            'email': email,
            'password': password,
            'webdav_url': self.webdav_url
        }
        
        with open(self.config_file, 'w', encoding='utf-8') as f:
            json.dump(config_data, f, ensure_ascii=False, indent=2)